/**
 * 
 */
package models;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;

import views.AddNewEventFrame;


/**
 * @author LAP
 *
 */
public class EventList {
	
	private static EventList self = new EventList();
	
	private EventList() {
		super();
		eventList = new LinkedList<EventData>();
	}
	
	private LinkedList<EventData> eventList;
	
	public static EventList getInstance() {
		return self;
	}
	
	public EventData addEvent(AddNewEventFrame frame) {
		if (frame == null) throw new NullPointerException("Frame must be not null");
		return addEvent(Generate(frame));		
	}
	
	public EventData addEvent(EventData event) {
		if (event == null) throw new NullPointerException("Event must be not null");
		eventList.add(event);
		return event;
	}
	
	private EventData Generate(AddNewEventFrame frame) { // Generates event from form data
		EventData event = new EventData(); // creates a new event
		Calendar fullEndDate = Calendar.getInstance();
		Calendar fullEndTime = Calendar.getInstance();
		fullEndTime.setTime((Date)frame.getEventEndTime().getValue());
		fullEndDate.setTime(frame.getEventEndDate().getDate());
		fullEndDate.set(
				Calendar.HOUR_OF_DAY, 
				fullEndTime.get(Calendar.HOUR_OF_DAY));
		fullEndDate.set(
				Calendar.MINUTE, 
				fullEndTime.get(Calendar.MINUTE));
		fullEndDate.set(
				Calendar.SECOND, 
				fullEndTime.get(Calendar.SECOND));
		fullEndDate.set(
				Calendar.MILLISECOND, 
				fullEndTime.get(Calendar.MILLISECOND));
		event.setFullEndDate(fullEndDate.getTime());
		event.setDescription(frame.getEventDescription().getText());
		event.setName(frame.getEventName().getText());
		return event;
	}
}
